package com.ds.dsfans.shell.util.adb.simulator;

import com.ds.dsfans.common.utils.StringUtils;
import com.ds.dsfans.shell.util.common.ShellUtil;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Value;

import java.util.ArrayList;
import java.util.List;

/**
 * mumu模拟器指令
 */
@Log
public class Mumu {
    @Value("${mumu.path}")
    private String mumuPath = "D:\\Program Files\\Netease\\MuMuPlayer-12.0\\shell\\";

    public String getMumuSimulator() {
        String mumuSimulator = "MuMuManager.exe";
        if (!StringUtils.isEmpty(mumuPath)) {
            if (mumuPath.endsWith("/") || mumuPath.endsWith("\\")) {
                mumuSimulator = "\"" + mumuPath + mumuSimulator + "\"";
            } else {
                mumuSimulator = "\"" + mumuPath + "\\" + mumuSimulator + "\"";
            }
        }
        return mumuSimulator;
    }

    /**
     * 获取模拟器列表
     *
     * @return
     */
    public List<Simulator> listSimulator() {
        List<Simulator> simulators = new ArrayList<>();
        String simulatorStr = ShellUtil.getShellOut(getMumuSimulator() + " adb api get_player_list");
        String[] ids = simulatorStr.split("\r\n");
        if (ids.length == 2) {
            //当前执行的模拟器编号
            String[] split = ids[0].split(" ");
            String selIndex = split[split.length - 1];
            //模拟器列表
            split = ids[1].split(" ");
            String indexList = split[3];
            if (indexList != null && indexList.startsWith("[")) {
                split = indexList.substring(1).split(",");
                for (int i = 0; i < split.length - 1; i++) {
                    Simulator simulator = new Simulator();
                    String index = split[i];
                    simulator.setIndex(index);
                    //选择状态
                    if (index.equals(selIndex)) {
                        simulator.setSelectStatus(true);
                    } else {
                        simulator.setSelectStatus(false);
                    }
                    //开启状态
                    boolean checkSimulator = checkSimulatorStatus(index);
                    simulator.setCheckStatus(checkSimulator);
                    simulators.add(simulator);
                }
            }

        }
        return simulators;
    }

    /**
     * 启动模拟器
     *
     * @param index
     * @return
     */
    public boolean startSimulator(String index) {
        //判断模拟器状态，如果没有启动则启动
        boolean result = false;
        if (!StringUtils.isEmpty(index)) {
            ShellUtil.getShellOut(getMumuSimulator() + " api -v " + index + " launch_player");
            result = checkSimulatorStatus(index);
        }
        return result;
    }

    /**
     * 检查模拟器的状态
     *
     * @param index
     * @return
     */
    public boolean checkSimulatorStatus(String index) {
        boolean result = false;
        if (!StringUtils.isEmpty(index)) {
            String simulatorStr = ShellUtil.getShellOut(getMumuSimulator() + "  api -v " + index + " player_state");
            String[] simulators = simulatorStr.split("\r\n");
            if (simulators != null && simulators.length > 0) {
                //模拟器未启动
                if (simulators[1].trim().equals("player not running")) {
                    return result;
                } else {
                    //等待模拟器启动
                    String status = simulators[1].split("=")[1];
                    if (status.equals("start_finished")) {
                        result = true;
                        return result;
                    } else {
                        //模拟器启动中，等待模拟器启动
                        log.info("模拟器" + index + "启动中....");
                        try {
                            Thread.sleep(5000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        checkSimulatorStatus(index);
                    }
                }
            }
        }
        return result;
    }

    /**
     * 关闭模拟器
     *
     * @param index
     * @return
     */
    public boolean stopSimulator(String index) {
        boolean result = false;
        if (!StringUtils.isEmpty(index)) {
            String simulatorStr = ShellUtil.getShellOut(getMumuSimulator() + " api -v " + index + " shutdown_player");
            String[] simulators = simulatorStr.split("\r\n");
            if (simulators != null && simulators.length > 0) {
                String[] temp = simulators[0].split(":");
                if (temp[0].trim().equals("current select player index") && temp[1].trim().equals(index)) {
                    result = true;
                }
            }
        }
        return result;
    }

    /**
     * 复制模拟器
     *
     * @param index
     * @return
     */
    public boolean copySimulator(String index) {
        boolean result = false;
        if (!StringUtils.isEmpty(index)) {
            String simulatorStr = ShellUtil.getShellOut(getMumuSimulator() + " api copy_player " + index);
            if (StringUtils.isNotEmpty(simulatorStr)) {
                result = true;
            }
        }
        return result;
    }

    /**
     * 删除模拟器
     *
     * @param index
     * @return
     */
    public boolean deleteSimulator(String index) {
        boolean result = false;
        if (!StringUtils.isEmpty(index)) {
            String simulatorStr = ShellUtil.getShellOut(getMumuSimulator() + " api delete_player " + index);
            if (StringUtils.isNotEmpty(simulatorStr)) {
                result = true;
            }
        }
        return result;
    }


    public static void main(String[] args) {
        Mumu mumu = new Mumu();
        // 开启 index=0;
        mumu.startSimulator("0");

        List<Simulator> simulators = mumu.listSimulator();
        for (int i = 0; i < simulators.size(); i++) {
            System.out.println(simulators.get(i).getIndex() + ":" + simulators.get(i).getSelectStatus() + ":" + simulators.get(i).getCheckStatus());
        }

        mumu.stopSimulator("0");
    }
}
